<template>
  <div>
    <el-form ref="deptForm" label-width="120px" :rules="rules" :model="form">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in list" :key="item.id" :value="item.username" :label="item.username" />
          <!-- <el-option value="2" label="大漂亮" /> -->
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
import { getData, addData } from '@/api/depDialog'
import { departmentlookdata, departmentChangdata } from '@/api/departments'

export default {
  props: {
    id: {
      required: true,
      type: String
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    oringin: {
      type: Array,
      required: true
    }
  },
  data() {
    const valiCode = (res, value, callback) => {
      let arr = this.oringin.map(e => e.code)
      if (this.isEdit) {
        arr = this.oringin.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (arr.includes(value)) {
        callback(new Error(`输入的${value}重复请重试`))
      } else {
        callback()
      }
    }
    const valiName = (res, value, callback) => {
      let arr2 = this.oringin.filter(item => item.pid === this.id).map(item => item.name)
      if (this.isEdit) {
        const pid = this.oringin.find(item => item.id === this.id).pid
        arr2 = this.oringin.filter(item => item.pid === pid).filter(item => item.id !== this.id).map(item => item.name)
      }
      console.log(arr2)
      if (arr2.includes(value)) {
        callback(new Error(`该部门名：${value}已存在`))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      list: [],
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 1, max: 15, message: '1-15的内容', trigger: 'blur' },
          { validator: valiName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 1, max: 15, message: '1-15的内容', trigger: 'blur' },
          { validator: valiCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '300的内容', trigger: 'blur' }
        ]
      }
    }
  },
  // watch: {
  //   id: {
  //     handler: (now, old) => {
  //       console.log(now)
  //       console.log(old)
  //       this.loaddepartmentlookdata()
  //     },
  //     immediate: true
  //   }
  // },
  created() {
    this.loadEploer()
    if (this.isEdit) {
      this.loaddepartmentlookdata()
    }
  },
  methods: {
    // 关闭清除表单
    resetfrom() {
      this.$refs.deptForm.resetFields()
    },
    // 编辑修改请求
    async  doEdit() {
      try {
        const { data: res } = await departmentChangdata(this.form)
        console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 提交请求
    hSubmit() {
      this.$refs.deptForm.validate((val) => {
        if (!val) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },

    // 关闭
    hCancel() {
      this.$emit('guanbi')
    },

    // 加载选中id的数据
    async  loaddepartmentlookdata() {
      try {
        const { data: res } = await departmentlookdata(this.id)
        console.log(res)
        if (res.code === 10000) {
          this.form = res.data
        }
      } catch (error) {
        console.log(error)
      }
    },

    // 加载编辑
    async loadEploer() {
      const { data: res } = await getData(this.form)
      console.log(res.data)
      this.list = res.data
    },

    // 添加请求
    async doAdd() {
      try {
        const params = { ...this.form, pid: this.id }
        const { data: rs } = await addData(params)
        // console.log(rs)
        if (rs.code !== 10000) return this.$message.error(rs.massage)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
